var xmlhttp;
if (window.XMLHttpRequest) {
	// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp = new XMLHttpRequest();
} else {
	// code for IE6, IE5
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

document.onkeydown = function(event) {
	e = event ? event : (window.event ? window.event : null);
	if (e.keyCode == 13) {
		// alert('回车检测到了');
		if (document.getElementById('login_div').style.display != "none") {
			document.getElementById('loginbutton').click();
		}
	}
}

/*
 * 点击 标签切换
 */
function tabClick(tabId) {
	if (tabId == "tabsName-1") {
		selectTabs("#tabs-1");
	}
	if (tabId == "tabsName-2") {
		selectTabs("#tabs-2");
	}
	if (tabId == "tabsName-3") {
		selectTabs("#tabs-3");
	}
	if (tabId == "tabsName-4") {
		selectTabs("#tabs-4");
	}
}

/*
 * 登录
 */
function login() {
	// alert("login");
	validateResult = validate();
	if (validateResult == '1') {
		alert("请输入验证码！");
		return false;
	} else if (validateResult == '2') {
		alert("验证码输入错误！");
		return false;
	} else if (validateResult == '3') {

	}
	var str = "";
	var elem = document.forms['loginForm'].elements;
	for ( var i = 0; i < elem.length; i++) {
		if (i != 0){
			str += "&";
		}
		str += elem[i].name + "=" + elem[i].value;
	}

	var calc = document.getElementById('Calc');
	var result = calc.POST("http://api.cntv.cn/client/auth", str);
	if (result == 200) {
		// alert(calc.GetResponse());
		// var json = eval('(' + calc.GetResponse() + ')');
		// var json = calc.GetResponse().parseJSON();
		var myJson = calc.GetResponse();
		var json = eval("(" + myJson + ")");
		// alert("login:ret:" + json.ret);
		if (json.ret == "succ") {
			// alert("login:ret:" + json.username);
			initPage();
		} else {
			alert(json.msg);
		}
	}

}

/*
 * 退出
 */
function loginout() {
	var str = "";
	xmlhttp.onreadystatechange = loginoutCallback;
	xmlhttp.open("GET", "http://api.cntv.cn/client/logout", false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
}
/*
 * 退出回调
 */
function loginoutCallback() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		initPage();
	}
}

/*
 * 单片添加
 */
function add() {
	var str = "";
	var elem = document.forms['addForm'].elements;
	for ( var i = 0; i < elem.length; i++) {
		if (i != 0)
			str += "&";
		str += elem[i].name + "=" + elem[i].value;
	}

	var calc = document.getElementById('Calc');
	var result = calc.POST("http://api.cntv.cn/client/add", str);
	// alert(result);
	// 返回-1是其他错误; 返回-2是C:\\WINDOWS\\system32\\drivers\\etc\\hosts不可读;
	// 返回-3是api.cntv.cn不通
	if (result == 200) {
		// alert(calc.GetResponse());
		// var json = eval('(' + calc.GetResponse() + ')');
		// var json = calc.GetResponse().parseJSON();
		var myJson = calc.GetResponse();
		var json = eval("(" + myJson + ")");
		alert(json.msg);
		location.reload();
	}

	/*
	 * xmlhttp.onreadystatechange = addCallback; xmlhttp.open("POST",
	 * "http://api.cntv.cn/client/add", false);
	 * xmlhttp.setRequestHeader("Content-Type",
	 * "application/x-www-form-urlencoded"); xmlhttp.send(str);
	 */
}

/*
 * 批量添加
 */
function batchadd() {
	var str = "";
	/* 获取选中的项 */
	var i = 0;
	$('.checkitem:checked').each(function() {
		if (i != 0) {
			str += "&";
		}
		url = document.getElementById("url_" + i).value;
		// title = document.getElementById("title_"+i).value;
		// alert(title);
		str += "&url[]=" + url + '';
		// str += "&title[]=" + title + '';
		i++;
	});
	cat_id = encodeURIComponent(document.getElementById("cat_id").value);
	str += "&cat_id=" + cat_id;
	repeat_id = document.getElementById("repeat_id").value;
	str += "&repeat_id=" + repeat_id;
	partner_id = document.getElementById("partner_id").value;
	str += "&partner_id=" + partner_id;
	album_id = document.getElementById("album_id").value;
	str += "&album_id=" + album_id;
	special_id = document.getElementById("special_id").value;
	str += "&special_id=" + special_id;
	if (document.getElementById("sendtype").checked) {
		sendtype = '1';
	}
	str += "&sendtype=" + sendtype;

	var calc = document.getElementById('Calc');
	var result = calc.POST("http://api.cntv.cn/client/batchadd", str);
	// alert(result);
	// 返回-1是其他错误; 返回-2是C:\\WINDOWS\\system32\\drivers\\etc\\hosts不可读;
	// 返回-3是api.cntv.cn不通
	if (result == 200) {
		// alert(calc.GetResponse());
		// var json = eval('(' + calc.GetResponse() + ')');
		// var json = calc.GetResponse().parseJSON();
		var myJson = calc.GetResponse();
		var json = eval("(" + myJson + ")");
		alert(json.msg);
		// document.getElementById('tabsName-2').click();
		localData.setItem("Arraytitle", null);
		localData.setItem("Arrayurl", null);
		// location.reload();
		selectTabs("#tabs-2");
	}

	/*
	 * xmlhttp.onreadystatechange = batchaddCallback; xmlhttp.open("POST",
	 * "http://api.cntv.cn/client/batchadd", false);
	 * xmlhttp.setRequestHeader("Content-Type",
	 * "application/x-www-form-urlencoded"); xmlhttp.send(str);
	 */
}
/*
 * 全选
 */
function checkall() {
	// alert("checkall");
	var code_Values = document.getElementsByClassName("checkitem");
	for (i = 0; i < code_Values.length; i++) {
		if (code_Values[i].type == "checkbox") {
			code_Values[i].checked = true;
		}
	}
}
/*
 * 取消全选
 */
function uncheckall() {
	var code_Values = document.getElementsByClassName("checkitem");
	for (i = 0; i < code_Values.length; i++) {
		if (code_Values[i].type == "checkbox") {
			code_Values[i].checked = false;
		}
	}
}

/*
 * 取得马甲信息
 */
function getrepeat() {

	var str = "";
	//xmlhttp.onreadystatechange = getrepeatCallback;
	xmlhttp.open("GET", "http://api.cntv.cn/client/getrepeat", false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
	getrepeatCallback();
}

/*
 * 取得马甲信息回调
 */
function getrepeatCallback() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		 //alert(xmlhttp.responseText);
		// initPage();
		// var json = JSON.parse(xmlhttp.responseText);
		var json = eval("(" + xmlhttp.responseText + ")");
		repeat_id_select = document.getElementById("repeat_id");
		repeat_id_select.options.length = 0;
		set_repeat_id_select = document.getElementById("set_repeat_id");
		set_repeat_id_select.options.length = 0;
		if (repeat_id_select != null) {
			repeat_id_select.options.add(new Option("选择马甲", "0"));
		}
		if (set_repeat_id_select != null) {
			set_repeat_id_select.options.add(new Option("选择马甲", "0"));
		}
		if (json.ret == 'succ') {
			// @TODO
			$.each(json.data, function(index, d) {
				if (repeat_id_select != null) {
					repeat_id_select.options.add(new Option(d.repeat_name,
							d.repeat_id));
				}
				if (set_repeat_id_select != null) {
					set_repeat_id_select.options.add(new Option(d.repeat_name,
							d.repeat_id));
				}
			});
		} else {
			if (repeat_id_select != null) {
				repeat_id_select.options.add(new Option("马甲1", "1"));
				repeat_id_select.options.add(new Option("马甲2", "2"));
			}
			if (set_repeat_id_select != null) {
				set_repeat_id_select.options.add(new Option("马甲1", "1"));
				set_repeat_id_select.options.add(new Option("马甲2", "2"));
			}
		}
	}
}

/*
 * 取得合作伙伴信息
 */
function getpartner() {
	var str = "";
	//xmlhttp.onreadystatechange = getpartnerCallback;
	xmlhttp.open("GET", "http://api.cntv.cn/client/getpartner", false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
	getpartnerCallback();

}

/*
 * 取得合作伙伴信息回调
 */
function getpartnerCallback() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		// var json = JSON.parse(xmlhttp.responseText);
		var json = eval("(" + xmlhttp.responseText + ")");
		partner_id_select = document.getElementById("partner_id");
		partner_id_select.options.length = 0;
		set_partner_id_select = document.getElementById("set_partner_id");
		set_partner_id_select.options.length = 0;
		if (partner_id_select != null) {
			partner_id_select.options.add(new Option("合作伙伴", "0"));
		}
		if (set_partner_id_select != null) {
			set_partner_id_select.options.add(new Option("合作伙伴", "0"));
		}
		if (json.ret == 'succ') {
			// @TODO
			$.each(json.data, function(index, d) {
				if (partner_id_select != null) {
					partner_id_select.options.add(new Option(d.title,
							d.partner_id));
				}
				if (set_partner_id_select != null) {
					set_partner_id_select.options.add(new Option(d.title,
							d.partner_id));
				}
			});
		} else {
			if (partner_id_select != null) {
				partner_id_select.options.add(new Option("合作伙伴1", "1"));
				partner_id_select.options.add(new Option("合作伙伴2", "2"));
			}
			if (set_partner_id_select != null) {
				set_partner_id_select.options.add(new Option("合作伙伴1", "1"));
				set_partner_id_select.options.add(new Option("合作伙伴2", "2"));
			}
		}
	}
}

/*
 * 取得视频列表
 */
function videolist(pagenum) {
	var str = "";
	//xmlhttp.onreadystatechange = getuserinfoCallback;
	xmlhttp.open("GET", "http://api.cntv.cn/client/videolist", false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
	videolistCallback();

}

/*
 * 取得视频列表回调
 */
function videolistCallback() {
	//alert("videolistCallback");

	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		//alert("videolistCallback:" + xmlhttp.responseText);
		// var data = JSON.parse(xmlhttp.responseText);
		var data = eval("(" + xmlhttp.responseText + ")");
		var tab = document.getElementById("videolist");
		var table = "<table class=\"tbl lstb\">";
		table += "<tr>";
		table += "<th>状态</th>";
		table += "<th>标题</th>";
		table += "<th>时间</th>";
		table += "<th>备注</th>";
		table += "</tr>";
		$.each(data.data, function(index, d) {
			// 视频采集状态 10：待采集 15：采集中 20：已采集30：采集失败 35：送审中 40：已送审50：送审失败
			if (d.status == '10') {
				var statusName = "待采集";
			} else if (d.status == '15') {
				var statusName = "采集中";
			} else if (d.status == '20') {
				var statusName = "已采集";
			} else if (d.status == '30') {
				var statusName = "采集失败";
			} else if (d.status == '35') {
				var statusName = "送审中";
			} else if (d.status == '40') {
				var statusName = "已送审";
			} else if (d.status == '50') {
				var statusName = "送审失败";
			} else {
				var statusName = "未知";
			}

			table += "<tr>";
			table += "<td>" + statusName + "</td>";
			table += "<td>" + d.title + "</td>";
			table += "<td>" + d.add_time + "</td>";
			table += "<td>" + d.url + "</td>";
			table += "</tr>";

		})
		table += "</table>";
		tab.innerHTML = table;

		var listinfo = "<span id=\"listtotal\">共" + data.total + "条信息</span>";

		if (data.currentpage > 1) {
			var prePage = data.currentpage - 1;
		} else {
			var prePage = 1;
		}
		if (data.currentpage >= data.totalpages) {
			var nextPage = data.totalpages;
		} else {
			var nextPage = data.currentpage + 1;
		}
		listinfo += "<a href=\"?page=" + prePage
				+ "#tabs-4\" id=\"firstButton\">|<<</a>";
		if (data.totalpages > 5) {
			var hasMore = true;

			var currentBlock = Math.floor(data.currentpage / 6) * 5 + 1;
			if ((currentBlock + 5) > data.totalpages) {
				var perBlock = data.totalpages + 1;
			} else {
				var perBlock = currentBlock + 5;
			}

			for ( var num = currentBlock; num < perBlock; num++) {
				if (data.currentpage == num) {
					listinfo += "<a href=\"?page="
							+ num
							+ "#tabs-4\" class=\"cur\" class=\"pageButton_class\" id=\"pageButton_"
							+ num + "\">" + num + "</a>";
				} else {
					listinfo += "<a href=\"?page="
							+ num
							+ "#tabs-4\" class=\"pageButton_class\"  id=\"pageButton_"
							+ num + "\">" + num + "</a>";
				}
			}
		} else {
			var perBlock = data.totalpages;
			var hasMore = false;
			for ( var num = 1; num <= perBlock; num++) {
				if (data.currentpage == num) {
					listinfo += "<a href=\"?page="
							+ num
							+ "#tabs-4\" class=\"cur\" class=\"pageButton_class\" id=\"pageButton_"
							+ num + "\">" + num + "</a>";
				} else {
					listinfo += "<a href=\"?page="
							+ num
							+ "#tabs-4\" class=\"pageButton_class\"  id=\"pageButton_"
							+ num + "\">" + num + "</a>";
				}
			}
		}
		if (data.currentpage >= data.totalpages) {
			listinfo += "<a href=\"?page=" + nextPage
					+ "#tabs-4\" id=\"lastButton\">>>|</a>";
		} else {
			listinfo += " ...<a href=\"?page=" + nextPage
					+ "#tabs-4\" id=\"lastButton\">>>|</a>";
		}

		var pageInfo = document.getElementById("listinfo");
		pageInfo.innerHTML = listinfo;

	}
}

/*
 * 取得queryString参数
 */
function GetRequest() {
	var url = location.search; // 获取url中"?"符后的字串
	var theRequest = new Object();
	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
		strs = str.split("&");
		for ( var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
		}
	}
	return theRequest;
}

/*
 * 单片提交页面
 */
function addTab() {
	// 转载本地存储到页面表单
	//alert(localData.getItem("title"));
	$("#title").val(localData.getItem("title"));

	$("#tag").val(localData.getItem("tag"));
	$("#intro").val(localData.getItem("intro"));

	cat_id_select = document.getElementById("cat_id");
	set_cat_id = localData.getItem("set_cat_id");
	for ( var i = 0; i < cat_id_select.options.length; i++) {
		if (cat_id_select.options[i].value == set_cat_id) {
			cat_id_select.options[i].selected = true;
		}
	}
	getrepeat();
	repeat_id_select = document.getElementById("repeat_id");
	for ( var i = 0; i < repeat_id_select.options.length; i++) {
		if (repeat_id_select.options[i].value == localData
				.getItem("set_repeat_id")) {
			repeat_id_select.options[i].selected = true;
		}
	}
	getpartner();
	partner_id_select = document.getElementById("partner_id");
	for ( var i = 0; i < partner_id_select.options.length; i++) {
		if (partner_id_select.options[i].value == localData
				.getItem("set_partner_id")) {
			partner_id_select.options[i].selected = true;
		}
	}
	cover_checkbox = document.getElementsByName("cover");
	// var coverAry = JSON.parse(localData.getItem("set_cover"));
	if (localData.getItem("set_cover") != null) {
		var coverAry = localData.getItem("set_cover").split(",");
		alert("test12");
		for ( var i = 0; i < cover_checkbox.length; i++) {
			for ( var j = 0; j < coverAry.length; j++) {
				if (cover_checkbox[i].value == coverAry[j]) {
					cover_checkbox[i].checked = true;
				}
			}

		}
	}
	// $("#cover").val(localData.getItem("set_cover"));
	$("#album_id").val(localData.getItem("set_album_id"));
	$("#special_id").val(localData.getItem("set_special_id"));

	$("#url").val(localData.getItem("url"));
	$("#sendtype").val(localData.getItem("sendtype"));
	$("#format").val(localData.getItem("format"));

}

/*
 * 发布设置页面
 */
function settingTab() {
	// 转载本地存储到页面表单
	cat_id_select = document.getElementById("set_cat_id");
	for ( var i = 0; i < cat_id_select.options.length; i++) {
		if (cat_id_select.options[i].value == localData.getItem("set_cat_id")) {
			cat_id_select.options[i].selected = true;
		}
	}
	getrepeat();
	repeat_id_select = document.getElementById("set_repeat_id");
	for ( var i = 0; i < repeat_id_select.options.length; i++) {
		if (repeat_id_select.options[i].value == localData
				.getItem("set_repeat_id")) {
			repeat_id_select.options[i].selected = true;
		}
	}
	getpartner();
	partner_id_select = document.getElementById("set_partner_id");
	for ( var i = 0; i < partner_id_select.options.length; i++) {
		if (partner_id_select.options[i].value == localData
				.getItem("set_partner_id")) {
			partner_id_select.options[i].selected = true;
		}
	}
	cover_checkbox = document.getElementsByName("set_cover");
	var coverAry = localData.getItem("set_cover");
	if (coverAry != null) {
		// var coverAryJson = JSON.parse(coverAry);
		var coverAryJson = coverAry.split(",");
		for ( var i = 0; i < cover_checkbox.length; i++) {
			for ( var j = 0; j < coverAryJson.length; j++) {
				if (cover_checkbox[i].value == coverAryJson[j]) {
					cover_checkbox[i].checked = true;
				}
			}

		}
	}

	// $("#cover").val(localData.getItem("set_cover"));
	$("#set_album_id").val(localData.getItem("set_album_id"));
	$("#set_special_id").val(localData.getItem("set_special_id"));
}

/*
 * 发布设置
 */
function setting() {
	// 表单设置到本地存储
	// 发送给扩展模块
	cat_id_select = document.getElementById("set_cat_id");
	var cat_id = cat_id_select.options[cat_id_select.options.selectedIndex].value

	repeat_id_select = document.getElementById("set_repeat_id");
	var repeat_id = repeat_id_select.options[repeat_id_select.options.selectedIndex].value

	partner_id_select = document.getElementById("set_partner_id");
	var partner_id = partner_id_select.options[partner_id_select.options.selectedIndex].value

	var coverAry = new Array();
	cover_checkbox = document.getElementsByClassName("set_cover");
	for ( var i = 0; i < cover_checkbox.length; i++) {
		if (cover_checkbox[i].checked) {
			coverAry.push(cover_checkbox[i].value);
		}
	}
	var partner_id = partner_id_select.options[partner_id_select.options.selectedIndex].value

	localData.setItem("set_cat_id", cat_id);
	localData.setItem("set_repeat_id", repeat_id);
	localData.setItem("set_partner_id", partner_id);
	localData.setItem("set_cover", eval(coverAry));
	localData.setItem("set_album_id", $("#set_album_id").val());
	localData.setItem("set_special_id", $("#set_special_id").val());
	alert("设置成功！");
}

/**
 * 框选需要
 * 
 * @returns {Base64}
 */
function Base64() {
	// private property
	_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	// public method for decoding
	this.decode = function(input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		while (i < input.length) {
			enc1 = _keyStr.indexOf(input.charAt(i++));
			enc2 = _keyStr.indexOf(input.charAt(i++));
			enc3 = _keyStr.indexOf(input.charAt(i++));
			enc4 = _keyStr.indexOf(input.charAt(i++));
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
			output = output + String.fromCharCode(chr1);
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
		}
		output = _utf8_decode(output);
		return output;
	}

	// private method for UTF-8 decoding
	_utf8_decode = function(utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while (i < utftext.length) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			} else if ((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i + 1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			} else {
				c2 = utftext.charCodeAt(i + 1);
				c3 = utftext.charCodeAt(i + 2);
				string += String.fromCharCode(((c & 15) << 12)
						| ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}
/*
 * 批量添加页面
 */
function batchaddTab() {
	var sHref = document.location.href
	var args = sHref.split("?");
	if (args[0] == sHref) /* 参数为空 */
	{
		return; /* 无需做任何处理 */
	}
	var str = args[1];
	args = str.split("&");
	var b = new Base64();

	var tab = document.getElementById("batchlist");
	// 如果是ie
	var table = "<table class=\"tbl lstb\" id=\"hmhm\">";
	table += "<tr>";
	table += "<th>选择</th>";
	table += "<th>标题</th>";
	table += "<th>url</th>";
	table += "</tr>";

	for ( var i = 0; i < args.length; i++) {
		str = args[i];
		var arg = str.split("|");
		if (arg.length <= 1)
			continue;
		addd(b.decode(arg[0]), b.decode(arg[1]));
		table += "<tr>";
		table += "<td><input  class=\"checkitem\"  type=\"checkbox\" value=\""
				+ i + "\"/ id=\"id_" + i + "\"></td>";
		table += "<td><input   type=\"hidden\" value=\"" + b.decode(arg[0])
				+ "\"/ id=\"title_" + i + "\">" + b.decode(arg[0]) + "</td>";
		table += "<td><input   type=\"hidden\" value=\"" + b.decode(arg[1])
				+ "\"/ id=\"url_" + i + "\">" + b.decode(arg[1]) + "</td>";
		table += "</tr>";
	}
	table += "</table>";
	var cat_id = localData.getItem("set_cat_id");
	var repeat_id = localData.getItem("set_repeat_id");
	var partner_id = localData.getItem("set_partner_id");
	var album_id = localData.getItem("set_album_id");
	var special_id = localData.getItem("set_special_id");
	var setDiv = "<div style='display:none;'>";
	setDiv += "<input type=\"text\" id=\"cat_id\" name=\"cat_id\" value=\""
			+ cat_id + "\" />";
	setDiv += "<input type=\"text\" id=\"repeat_id\" name=\"repeat_id\" value=\""
			+ repeat_id + "\" />";
	setDiv += "<input type=\"text\" id=\"partner_id\" name=\"partner_id\" value=\""
			+ partner_id + "\" />";
	setDiv += "<input type=\"text\" id=\"album_id\" name=\"album_id\" value=\""
			+ album_id + "\" />";
	setDiv += "<input type=\"text\" id=\"special_id\" name=\"special_id\" value=\""
			+ special_id + "\" />";
	setDiv += "</div>";
	tab.innerHTML = table + setDiv;
	var hmnum = document.getElementById("hmnum");
	hmnum.innerText = "共" + AurlJson.length + "条信息"; // alert(Aurl.length);
}
/*
 * 过滤url
 */
function filterBykey() {
	var tab = document.getElementById("batchlist");
	var filterKey = document.getElementById("filterKey").value;
	// 如果是ie
	var table = "<table class=\"tbl lstb\" id=\"hmhm\">";
	table += "<tr>";
	table += "<th>选择</th>";
	table += "<th>标题</th>";
	table += "<th>url</th>";
	table += "</tr>";

	var listIndex = 0;
	var Arraytitle = localData.getItem("Arraytitle");
	var Aurl = localData.getItem("Arrayurl");
	if (Aurl != null && Arraytitle != null && Aurl != "" && Arraytitle != "") {
		var ArraytitleJson = JSON.parse(Arraytitle);
		var AurlJson = JSON.parse(Aurl);
		for ( var i = 0; i < AurlJson.length; ++i) {
			if (AurlJson[i].indexOf(filterKey) >= 0) {
				table += "<tr>";
				table += "<td><input  class=\"checkitem\"  type=\"checkbox\" value=\""
						+ i + "\"/ id=\"id_" + listIndex + "\"></td>";
				table += "<td><input   type=\"hidden\" value=\""
						+ ArraytitleJson[i] + "\"/ id=\"title_" + listIndex
						+ "\">" + ArraytitleJson[i] + "</td>";
				table += "<td><input   type=\"hidden\" value=\"" + AurlJson[i]
						+ "\"/ id=\"url_" + listIndex + "\">" + AurlJson[i]
						+ "</td>";
				table += "</tr>";
				listIndex++;
			}

		}
		table += "</table>";
		var cat_id = localData.getItem("set_cat_id");
		var repeat_id = localData.getItem("set_repeat_id");
		var partner_id = localData.getItem("set_partner_id");
		var album_id = localData.getItem("set_album_id");
		var special_id = localData.getItem("set_special_id");
		var setDiv = "<div style='display:none;'>";
		setDiv += "<input type=\"text\" id=\"cat_id\" name=\"cat_id\" value=\""
				+ cat_id + "\" />";
		setDiv += "<input type=\"text\" id=\"repeat_id\" name=\"repeat_id\" value=\""
				+ repeat_id + "\" />";
		setDiv += "<input type=\"text\" id=\"partner_id\" name=\"partner_id\" value=\""
				+ partner_id + "\" />";
		setDiv += "<input type=\"text\" id=\"album_id\" name=\"album_id\" value=\""
				+ album_id + "\" />";
		setDiv += "<input type=\"text\" id=\"special_id\" name=\"special_id\" value=\""
				+ special_id + "\" />";
		setDiv += "</div>";
		tab.innerHTML = table + setDiv;
		var hmnum = document.getElementById("hmnum");
		hmnum.innerText = "共" + AurlJson.length + "条信息"; // alert(Aurl.length);
	} else {
		table += "</table>";
		tab.innerHTML = table;
	}

}

/*
 * 取得用户信息
 */
function getuserinfo() {
	//alert("getuserinfo");

	var str = "";
	//xmlhttp.onreadystatechange = getuserinfoCallback;
	xmlhttp.open("GET", "http://api.cntv.cn/client/getuserinfo", false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
	getuserinfoCallback();

}
/*
 * 取得用户信息回调
 */
function getuserinfoCallback() {
	//alert("getuserinfoCallback");
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		alert("getuserinfoCallback"+xmlhttp.responseText);
		var username = '';
		// var json = JSON.parse(xmlhttp.responseText);
		var json = eval("(" + xmlhttp.responseText + ")");
		username = json.username;
		//username = "12345";
		//alert(username);
		//alert(typeof (username));
		if (typeof (username) == "undefined" || username == "") {
			$("#tabs_div").hide();
			createCode();
			$("#login_div").show();
			alert("userinfo faild");
		} else {
			$("#login_div").hide();
			$("#uname").text(username);
			$("#tabs_div").show();
			selectTabs(window.location.hash);
			
			
			alert("userinfo succ");
		}
	}
}


/*
 * 初始化
 */
function initPage() {
	// 判断是否登录
	// alert("initPage");
	// alert(localData.getItem("title"));
	$("#tabs_div").hide();
	$("#login_div").hide();
	getuserinfo();
}

/*
 * tabs切换
 */
function selectTabs(tabIndex) {
	$("#tabs-1").hide();
	$("#tabs-2").hide();
	$("#tabs-3").hide();
	$("#tabs-4").hide();

	$("#tabsLi-1").removeClass("cur");
	$("#tabsLi-2").removeClass("cur");
	$("#tabsLi-3").removeClass("cur");
	$("#tabsLi-4").removeClass("cur");

	// alert(tabIndex);
	if (tabIndex == '#tabs-1') {
		// alert(tabIndex);
		$("#tabs-1").show();
		$("#tabsLi-1").addClass("cur");
		addTab();
	}
	if (tabIndex == '#tabs-2') {
		// alert(tabIndex);
		$("#tabs-2").show();
		$("#tabsLi-2").addClass("cur");
		batchaddTab();
	}
	if (tabIndex == '#tabs-3') {
		// alert(tabIndex);
		$("#tabs-3").show();
		$("#tabsLi-3").addClass("cur");
		settingTab();
	}
	if (tabIndex == '#tabs-4') {
		// alert(tabIndex);
		$("#tabs-4").show();
		$("#tabsLi-4").addClass("cur");
		var Request = new Object();
		Request = GetRequest();
		page = Request['page'];
		if (!page) {
			page = 1;
		}
		videolist(page);
	}
}
/*
 * 入口程序
 */
$(document).ready(function() {
	initPage();
});

